﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Office.Interop.Excel;


namespace MISAPP.Classify.Service
{
    public partial class Squared_Excel
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_cfg"></param>
        /// <param name="p_path"></param>
        /// <param name="p_category">簽帳金額</param>
        /// <param name="p_shtName"></param>
        /// <returns></returns>
        public bool UploadCardAmount(Service_CFG004 p_cfg,string p_path
            ,string[] p_category,string[] p_amount,string p_shtName)
        {
            bool result = false;
            try
            {   
                openFile(p_path);
                Worksheet wsht = getSheetObject(p_shtName) as Worksheet;
                p_cfg.Check(p_shtName);
                for (int i = 0; i < p_category.Length; i++)
                {
                    string[] tmp = p_category[i].Split(new char[] { ',' });
                    Range rng1 = wsht.get_Range(tmp[1], Type.Missing);
                    Range rng2 = wsht.get_Range(p_amount[i], Type.Missing);
                    object[,] cardTypes = rng1.Value2 as object[,];
                    object[,] amount = rng2.Value2 as object[,];
                    if (amount == null)
                    {
                        amount = new object[2,2] ;
                        cardTypes = new object[2,2];
                        amount[1, 1] = rng2.Value2;
                        cardTypes[1, 1] = rng1.Value2;
                    }
                    result = p_cfg.Insert(tmp[0], p_shtName, amount, cardTypes);                    
                }
                closeWorkBooksWithNoSave(p_path);                             
                result = true;
            }
            catch (Exception ex)
            {
                Utility.ReleaseCOMObject(exlAPP);
            }            
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_cfg"></param>
        /// <param name="p_file"></param>
        /// <param name="p_yyy">台灣年</param>
        public void GetCardAmount(Service_CFG004 p_cfg
            , string p_file,string p_yyy)
        {
            openFile(p_file);
            System.Data.DataTable dt = p_cfg.GetSummaryData(p_yyy);
            Range rng = null;
            Worksheet wsht = getSheetObject("AMOUNT") as Worksheet;
            //step1 先寫入 column
            int icol,irow;
            icol = 1;
            irow = 2;
            foreach (DataColumn col in dt.Columns)
            {
                rng = wsht.Cells[irow,icol] as Range;
                rng.Value2 = col.ColumnName;
                icol++;
            }
           
            irow = 3;
            icol = 1;
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    rng = wsht.Cells[irow, i+1] as Range;
                    rng.Value2 = row[i].ToString();
                }
                irow++;
            }
            Workbook wbk = exlAPP.Workbooks[1];
            wbk.Save();
            closeWorkBooksWithNoSave(p_file);
        }

        public void WMG005(Service_WMG005 p_wmg, string p_file
            , string p_ym,string p_cur)
        {
            openFile(p_file);
            Worksheet sht = getSheetObject("CUR") as Worksheet;
            Range rng = null;
            rng = sht.get_Range("A2", Type.Missing);
            rng.Value2 = p_wmg.GetCellYM(p_ym);
            string[] columns = p_wmg.GetCURColumn();
            //月份：100年3月份 A2
            System.Data.DataTable dt = p_wmg.GetSummaryData(p_ym,p_cur);
            int iRow;
            iRow = 5;
            string cell;
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    cell = string.Format("{0}{1}", columns[i] , iRow.ToString());
                    rng = sht.get_Range(cell, Type.Missing);
                    rng.Value2 = row[i];
                }
                iRow++;
            }

            Workbook wbk = exlAPP.Workbooks[1];
            wbk.Save();
            closeWorkBooksWithNoSave(p_file);
        }

        public bool GetBudgetResult(string p_templatePath, Service_Bonus p_bonus)
        {
            string[] shtCollect = Utility.GetSheets("BUDGET");
            return GetBonusResult(p_templatePath, p_bonus
                , shtCollect, null);
        }


        public bool GetSquaredResult4Download(string p_tmpFilePath)
        {

            return true;    
        }
    }
}
